//缓存查询内容
// import { window } from 'vscode';
import NodeCache from 'node-cache';
const globalCache = new NodeCache();
import { alitranslateAsync } from './aliTranslate';
import { youdaoTranslate } from './youdao';
import { aliProfessionalTranslate } from './aliTranslate';
import fs from 'fs';
import path from 'path';
let upstreamDir = path.dirname(__dirname); // 获取当前文件的上级目录
// 创建目录
let createDir = path.join(upstreamDir, 'cacheData'); // 创建目录路径

if (!fs.existsSync(createDir)) {
	fs.mkdir(createDir, { recursive: true }, (err) => {
		if (err) {
			console.error('Error creating directory:', err);
		} else {
			console.log('Directory created successfully:', createDir);
		}
	});
}
const cacheFilePath = path.join(createDir, 'cacheDataReadmeTranslate.json');
console.log('cacheFilePath:', cacheFilePath);

// 将缓存数据保存到文件
export function loadCacheFromFile() {
	if (fs.existsSync(cacheFilePath)) {
		const data = fs.readFileSync(cacheFilePath, 'utf-8');
		const cacheData = JSON.parse(data);
		for (const [key, value] of Object.entries(cacheData)) {
			globalCache.set(key, value);
		}

		// console.log('Cache loaded from file');
	} else {
		console.log('No cache file found, starting with an empty cache');
	}
}

export function saveCacheToFile() {
	const cacheData = globalCache.keys().reduce((obj, key) => {
		obj[key] = globalCache.get(key);
		return obj;
	}, {});
	fs.writeFileSync(cacheFilePath, JSON.stringify(cacheData, null, 2));
	// console.log('Cache saved to file');
}

export function getDataFromCache(key) {
	const data: any = globalCache.get(key) ?? null;
	if (data) {
		return data;
	}
	return data;
}

export function setDataToCache(key, data) {
	// console.log('Setting data to cache for key:', key);
	globalCache.set(key, data);
	saveCacheToFile();
}
export function getCurrentCount() {
	let currentCount = globalCache.get('currentService') ?? null;
	return currentCount;
}
export async function TranslatorBalancer(text: string) {
	let currentCount = getCurrentCount();
	if (currentCount === null) {
		setDataToCache('currentService', 0);
		// console.log('currentServicesCount is null');
	}
	if (currentCount === 0) {
		// console.log('currentCount:', currentCount);
		currentCount++;
		setDataToCache('currentService', currentCount);
		return await youdaoTranslate(text);
	} else if (currentCount === 1) {
		// console.log('currentCount:', currentCount);
		currentCount++;
		setDataToCache('currentService', currentCount);
		return await alitranslateAsync(text);
	} else if (currentCount === 2) {
		// console.log('currentCount:', currentCount);
		currentCount = 0;
		setDataToCache('currentService', currentCount);
		return await aliProfessionalTranslate(text);
	}
}
export async function translate(text: string) {
	if (text) {
		try {
			if (text.length <= 5000) {
				let result = TranslatorBalancer(text);
				return result;
			} else if (text.length > 5000) {
				let result1 = await TranslatorBalancer(text.substring(0, 5000));
				let result2 = await TranslatorBalancer(text.substring(5000, text.length));
				return result1 + result2;
			} else if (text.length > 10000) {
				console.log('The text is too long, please enter a shorter text');
				return null;
			}
		} catch (e: any) {
			console.log(e);
		}
	} else {
		return null;
	}
}
